package com.ts.service.achievement.impl;

import com.ts.dto.MemberDTO;
import com.ts.dto.ScientificResearchProjectDTO;
import com.ts.entity.Member;
import com.ts.entity.ScientificResearchProject;
import com.ts.mapper.achievement.ScientificResearchProjectMapper;
import com.ts.mapper.achievement.ScientificResearchProjectMemberMapper;
import com.ts.service.achievement.ScientificResearchProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScientificResearchProjectServiceImpl implements ScientificResearchProjectService {

    @Autowired
    private ScientificResearchProjectMapper scientificResearchProjectMapper;

    @Autowired
    private ScientificResearchProjectMemberMapper scientificResearchProjectMemberMapper;

    /**
     * 保存科研项目
     */
    @Transactional
    @Override
    public void save(ScientificResearchProjectDTO scientificResearchProjectDTO) {
        // 1.准备数据
        ScientificResearchProject scientificResearchProject = new ScientificResearchProject();
        BeanUtils.copyProperties(scientificResearchProjectDTO, scientificResearchProject);

        // 2.手动添加公共字段
        scientificResearchProject.setCreateTime(LocalDateTime.now());
        scientificResearchProject.setUpdateTime(LocalDateTime.now());
        scientificResearchProject.setCreateBy(1L);
        scientificResearchProject.setUpdateBy(1L);

        // 3.保存科研项目主表
        scientificResearchProjectMapper.save(scientificResearchProject);

        // 4.处理成员信息
        saveMembers(scientificResearchProjectDTO.getMemberList(), scientificResearchProject.getId());
    }

    /**
     * 根据ID查询科研项目详情
     */
    @Override
    public ScientificResearchProjectDTO getById(Long id) {
        // 1.查询项目主表
        ScientificResearchProject project = scientificResearchProjectMapper.getById(id);
        if (project == null) {
            return null;
        }

        // 2.转换为DTO
        ScientificResearchProjectDTO dto = new ScientificResearchProjectDTO();
        BeanUtils.copyProperties(project, dto);

        // 3.查询成员信息
        List<Member> members = scientificResearchProjectMemberMapper.getByProjectId(id);
        List<MemberDTO> memberDTOs = members.stream()
                .map(member -> {
                    MemberDTO memberDTO = new MemberDTO();
                    BeanUtils.copyProperties(member, memberDTO);
                    return memberDTO;
                })
                .collect(Collectors.toList());
        dto.setMemberList(memberDTOs);
        return dto;
    }

    /**
     * 更新科研项目
     */
    @Transactional
    @Override
    public void update(ScientificResearchProjectDTO scientificResearchProjectDTO) {
        // 1.更新项目主表
        ScientificResearchProject scientificResearchProject = new ScientificResearchProject();
        BeanUtils.copyProperties(scientificResearchProjectDTO, scientificResearchProject);
        scientificResearchProject.setUpdateTime(LocalDateTime.now());
        scientificResearchProject.setUpdateBy(1L);

        scientificResearchProjectMapper.update(scientificResearchProject);

        // 2.先删除原有成员，再重新插入
        Long projectId = scientificResearchProject.getId();
        scientificResearchProjectMemberMapper.deleteByProjectId(projectId);

        // 3.插入新的成员信息
        saveMembers(scientificResearchProjectDTO.getMemberList(), projectId);
    }

    /**
     * 根据ID删除科研项目
     */
    @Transactional
    @Override
    public void delete(Long id) {
        // 先删除成员信息
        scientificResearchProjectMemberMapper.deleteByProjectId(id);
        // 再删除项目主表
        scientificResearchProjectMapper.delete(id);
    }

    /**
     * 保存成员信息的私有方法
     */
    private void saveMembers(List<MemberDTO> memberDTOs, Long projectId) {
        if (memberDTOs != null && !memberDTOs.isEmpty()) {
            List<Member> members = memberDTOs.stream()
                    .map(memberDTO -> {
                        Member member = new Member();
                        BeanUtils.copyProperties(memberDTO, member);
                        member.setProjectId(projectId);
                        return member;
                    })
                    .collect(Collectors.toList());

            // 批量保存成员信息
            scientificResearchProjectMemberMapper.batchInsert(members);
        }
    }
}